﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SQLite;
using Com.Toook.Common;

public partial class UserControls_JokeBottomControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitRecommend();
    }
    /// <summary>
    /// 随机推荐同类笑话
    /// </summary>
    private void InitRecommend()
    {
        string type = Request["type"];
        int total = 1;
        int typeId = 0;
        string typeCondition = "";
        DataRow row = null;
        if (string.IsNullOrEmpty(type))//详细页面，没有type，按全部随机推荐
        {
            row = PageData.SelectParamRow("typeCode='joke'");
        }
        else//list页面按类型随机推荐。
        {
            typeId = ParamHelper.ParseIntPositive(type);
            row = PageData.SelectParamRow("typeCode='joke" + typeId.ToString() + "'");
            if (row == null)//没有找到，类型不对.设置为默认
            {
                row = PageData.SelectParamRow("typeCode='joke'");
            }
            else
            {
                typeCondition = " where type=" + typeId.ToString();
            }
        }
                
        int showCount = 12;//随机推荐显示数
        total = int.Parse(row[PageColumn.totalCount].ToString());
        int start = 0;
        if (total > showCount)
        {
            Random rand = new Random(DateTime.Now.Second + 157);
            start = rand.Next(total - showCount);
        }

        string sql = "select id, title from JokePublish " + typeCondition +
            " limit " + start.ToString() + "," + showCount.ToString();
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, conn);
        SQLiteDataReader reader = null;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            rptRecommend.DataSource = reader;
            rptRecommend.DataBind();
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, reader);
        }
    }
}
